package com.tb.sng.utils.controls
{	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;

	/**
	 * 数据loading显示
	 *
	 * @author 任冬 rendong01@snda.com
	 * $Id: DataLoading.as 9 2012-08-12 17:04:32Z rendong237@gmail.com $
	 * @version 1.0
	 */
	public class DataLoading
	{
		/**
		 * 保存配置信息
		 */
		private static var _config:Object = {};
		
		/**
		 * 记录添加loading的mc
		 */
		private static var _displayObject:Dictionary = new Dictionary();
		
		/**
		 * 设置风格资源 
		 * @param res
		 * @param style
		 * 
		 */		
		public static function setRes(res:Class, style:String = "default"):void
		{
			_config[style] = res;
		}
		
		
		/**
		 * 显示数据加载提示
		 * 
		 * @param	mc	显示加载动画的mc
		 * @param	width	宽度,当程序不能正确获取容器的大小时设置
		 * @param	height	高度,当程序不能正确获取容器的大小时设置
		 * @param	x		x坐标
		 * @param	y		y坐标
		 * @param	alpha	透明度
		 */
		public static function show(mc:DisplayObjectContainer, width:int = 0, height:int = 0, x:int = 0, y:int = 0, alpha:Number = 0.2, style:String = "default"):void
		{			
			var loadingMoveClass:Class = _config[style];
			
			if (loadingMoveClass == null)
				return;
			
			
			if (_displayObject[mc] && _displayObject[mc]['mc'])
				return;
			
			// 背景颜色 透明层
			var bgMc:Sprite;
			var loadingMc:MovieClip;
			
			// 记录第一次的大小
			if (width == 0)
			{
				if (_displayObject[mc] && _displayObject[mc]['width'])
					width = _displayObject[mc]['width'];
				else
					width = mc.width / mc.scaleX;
			}
			
			if (height == 0)
			{
				if (_displayObject[mc] && _displayObject[mc]['height'])
					height = _displayObject[mc]['height'];
				else
					height = mc.height/mc.scaleY;
			}
			
			bgMc = new Sprite();
			bgMc.graphics.beginFill(0x000, alpha);
			bgMc.graphics.drawRect(0, 0, width, height);
			bgMc.graphics.endFill();
			
			// 加载动画
			loadingMc = new loadingMoveClass();
				
			/*loadingMc.x = bgMc.width / 2 - loadingMc.width / 2;
			loadingMc.y = bgMc.height / 2 - loadingMc.height / 2;*/
			loadingMc.x = bgMc.width / 2 ;
			loadingMc.y = bgMc.height / 2 ;
			bgMc.addChild(loadingMc);
			
			bgMc.x = x;
			bgMc.y = y;
				
			mc.addChild(bgMc);
			
			_displayObject[mc] = {width:width, height:height, mc:bgMc};
		}
		
		
		/**
		 * 隐藏加载的loading
		 * 
		 * @param	mc	显示加载动画的mc
		 */
		public static function hide(mc:DisplayObjectContainer):void
		{
			var obj:Object = _displayObject[mc];
			
			
			if (obj && obj['mc'])
			{
				var disp:DisplayObject =  obj['mc'] as DisplayObject;
				
				if (disp && mc.contains(disp))
				{
					mc.removeChild(obj['mc']);
					delete obj['mc'];
					obj['mc'] = null;
				}
			}
		}
	}
}